feat(tools): apply_patch — git-style unified-diff edit tool - #39
Merged
Conversation
Adds a targeted edit tool so agents stop reading+rewriting whole files to change a few lines. - UnifiedDiff: Foundation-only parser + applier. Applies each hunk by matching its context (the @@ line numbers are hints, not authority), with nearest-hint selection and cumulative offset across hunks. All-or-nothing: any hunk that doesn't match leaves the file untouched. Preserves trailing-newline state. - PatchFileTool (apply_patch): reads an existing file, applies a unified diff, writes atomically. requiresConfirmation (the diff shows in the approval sheet). Clear errors name the first unmatched hunk so the model can re-read and regenerate. Robustness: wrong @@ numbers still apply via context (the property that makes LLM-generated diffs usable). Tests cover offset drift, nearest-hint, context not found → unchanged, trailing-newline preservation, insertion/removal. Full suite: 155 green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
apply_patch(path, patch)— a targeted edit tool. Agents can change a few lines by emitting a unified diff instead of reading the whole file and rewriting it withwrite_file(the cause of the read/rewrite storm on multi-file edits).How
UnifiedDiff(pure, Foundation-only) — parses a git/diff -upatch and applies each hunk by matching its context; the@@line numbers are treated as hints (nearest-hint selection + cumulative offset across hunks). All-or-nothing: any hunk that doesn't match leaves the file untouched. Trailing-newline state preserved.PatchFileTool(apply_patch) — reads an existing file, applies the diff, writes atomically.requiresConfirmation(diff shows in the approval sheet). Errors name the first unmatched hunk so the model can re-read and regenerate.Why context-matching (not strict git apply)
LLMs frequently get
@@line numbers wrong; strictgit applythen rejects the whole patch. Matching on context (like Aider / Codexapply_patch/patch -F) makes LLM-generated diffs reliable — a test asserts a patch with deliberately wrong line numbers still applies.Scope (v1)
Single existing file per call; text hunks only (no binary/rename/create — creation stays
write_file). Pure-Swift, no shelling togit/patch(keeps SwiftAgentKitTools Foundation-only + cross-platform).Testing
✅ Full suite 155 green, incl.
UnifiedDiffTests(wrong line numbers, offset drift, nearest-hint, context-not-found→unchanged, trailing-newline, insert/remove-only) andapply_patchtool cases (in-place edit, missing file, non-matching hunk leaves file unchanged, requiresConfirmation).Consumed by Naseem next (register the tool + prompt nudge) after an alpha bump. Spec in the Naseem repo:
docs/superpowers/specs/2026-08-04-apply-patch-tool-design.md.🤖 Generated with Claude Code